package com.ajouroid.smartcoworker;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.util.List;

import android.content.Context;
import android.graphics.ImageFormat;
import android.graphics.Rect;
import android.graphics.YuvImage;
import android.hardware.Camera;
import android.hardware.Camera.CameraInfo;
import android.hardware.Camera.PreviewCallback;
import android.util.AttributeSet;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

class CamScreen extends SurfaceView implements SurfaceHolder.Callback {

	public CamScreen(Context context) {
		super(context);
		mHolder = this.getHolder();
		mHolder.addCallback(this);
	}
	
	public CamScreen(Context context, AttributeSet attrs) {
		super(context, attrs);
		mHolder = this.getHolder();
		mHolder.addCallback(this);
	}

	public CamScreen(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		mHolder = this.getHolder();
		mHolder.addCallback(this);
	}

	SurfaceHolder mHolder;
	private Camera mCamera;
	Context mContext;
	
	Socket mConnection;
	private boolean recording = false;
	final String TAG = "VideoSender";
	private static final int SAMPLE_INTERVAL = 500; // milliseconds
	private byte[] imageBytes = null;// SAMPLE_INTERVAL *
	public void init(Socket connection)
	{
		mConnection = connection;
	}

	public void surfaceDestroyed(SurfaceHolder holder) {
		holder.removeCallback(this);
	        if (mCamera != null)
			{
				mCamera.setPreviewCallback(null);
				mCamera.stopPreview();
				mCamera.release();
				mCamera = null;
			}
	}

	public void surfaceChanged(SurfaceHolder holder, int format, int w,
			int h) {
		synchronized(this) {
			if (mCamera != null)
			{
				Camera.Parameters parameters = mCamera.getParameters();
		
				List<Camera.Size> previewSizes = parameters
						.getSupportedPreviewSizes();
				Camera.Size cs = previewSizes.get(0);
		
				parameters.setPictureSize(cs.width, cs.height);
				parameters.setPreviewSize(cs.width, cs.height);
				// parameters.setPreviewFrameRate(1);
		
				mCamera.setParameters(parameters);
				mCamera.startPreview();
			}
		}
	}
	  
	public void start()
	{
		SendVideo();
	}
	
	public void stop()
	{
		recording = false;
	}

	public void SendVideo() {
		recording = true;
		
		Thread thrd = new Thread(new Runnable() {
			public void run() {
				Log.e(TAG, "Ready to send video, thread id: "
						+ Thread.currentThread().getId());

				try {
					ObjectOutputStream oos = new ObjectOutputStream(mConnection.getOutputStream());
					
					while (recording) {
						if (imageBytes != null) {
							oos.writeObject(imageBytes);
							oos.flush();
							//Log.d(TAG, "Video sent: " + imageBytes.length);
						}
						Thread.sleep(SAMPLE_INTERVAL, 0);
					}
				} catch (InterruptedException ie) {
					Log.e(TAG, "InterruptedException");
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			} // end run
		});
		thrd.start();
	}

	
	public void surfaceCreated(SurfaceHolder holder) {
		synchronized(this)
		{
			mCamera = Camera.open(CameraInfo.CAMERA_FACING_FRONT);
			
	
			try {
				mCamera.setPreviewDisplay(mHolder);
			} catch (IOException exception) {
				exception.printStackTrace();
			}
			
		
			mCamera.setPreviewCallback(new PreviewCallback() {
				public void onPreviewFrame(byte[] data, Camera arg1) {
					long curTime = System.currentTimeMillis();
					
					if (curTime - prevTime > SAMPLE_INTERVAL)
					{
						int cWidth = arg1.getParameters().getPreviewSize().width;
						int cHeight = arg1.getParameters().getPreviewSize().height;
			
						ByteArrayOutputStream out = new ByteArrayOutputStream();
						YuvImage yuvImage = new YuvImage(data, ImageFormat.NV21,
								cWidth, cHeight, null);
						yuvImage.compressToJpeg(new Rect(0, 0, cWidth, cHeight),
								10, out);
						imageBytes = out.toByteArray();
						prevTime = System.currentTimeMillis();
					}
				}
			});
		}		
	}

	long prevTime = -1000;

}
